/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.utils;

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import cn.qidisoft.core.runtime.ws.QdfWsProxyFactory;
import cn.qidisoft.core.util.DateUtil;
import cn.qidisoft.core.util.Md5;
import cn.qidisoft.core.util.StringUtils;
import cn.qidisoft.edu.hzjt.constants.Constant;
import cn.qidisoft.edu.hzjt.controller.BaseController;

/**
 * 
 * @author hxq
 * @version $Id: AddSoapHeader.java, v 0.1 2018年4月9日 上午10:34:44 hxq Exp $
 */
public class AddSoapHeader extends AbstractSoapInterceptor {
  private static String userid;
  private static String pwd;

  // public AddSoapHeader(String userName, String password) {
  // super(Phase.WRITE);
  // }
  public AddSoapHeader() {
    super(Phase.WRITE);
  }

  /**
   * @see org.apache.cxf.interceptor.Interceptor#handleMessage(org.apache.cxf.message.Message)
   */
  @Override
  public void handleMessage(SoapMessage message) throws Fault {
    // SoapHeader部分待添加的节点
    QName qName = new QName("Authentication");
    Document doc = DOMUtils.createDocument();
    // 验证用户名
    Element userElement = doc.createElement("User");
    userElement.setTextContent(AddSoapHeader.userid);
    // 验证密码
    Element pwdElement = doc.createElement("Password");
    pwdElement.setTextContent(AddSoapHeader.pwd);
    Element root = doc.createElementNS("http://tempuri.org/", "Authentication");
    root.appendChild(userElement);
    root.appendChild(pwdElement);
    // 创建SoapHeader内容
    SoapHeader header = new SoapHeader(qName, root);
    List<Header> headers = message.getHeaders();
    headers.add(header);
  }

  /**
   * Header头信息设置
   * 
   * @param userName
   * @param client
   * @return
   */
  public static boolean setSoapHeader(String userName, Client client) {
    try {
      AddSoapHeader.userid = userName;
      AddSoapHeader.pwd = Md5.getMd5(Constant.WS_FLOW_SERVICE_KEY + userName
          + DateUtil.formatDate2Str(DateUtil.getCurrentDate(), "yyyyMMdd")).toUpperCase();
      return client.getOutInterceptors().add(new AddSoapHeader());
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }

  /**
   * 验证流程服务器是否连接
   * 
   * @param cellId
   * @param userName
   * @return
   */
  public static Client validateClient(String clientAddress, String userName) {
    if (!StringUtils.isNullValue(clientAddress)) {
      Client client = QdfWsProxyFactory.getBizInstance(clientAddress);
      if (client == null) {
        BaseController.setParameter("msg", "调用流程系统服务器连接失败");
        return client;
      } else {
        boolean flowState = setSoapHeader(userName, client);
        if (flowState == false) {
          BaseController.setParameter("msg", "身份校验在流程系统中验证失败，可能没配置流程");
          return null;
        } else {
          return client;
        }
      }
    } else {
      BaseController.setParameter("msg", "调用流程接口地址未配置，请求失败");
      return null;
    }
  }
}
